package com.paipaideli.ui.mine.order;

/**
 * Created by 物流信息 on 2019/8/31.
 */

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.mine.order.bean.LogisticsBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LogisticsActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.recycleview_logistics)
    PullRefreshView recycleview_logistics;

    private LogisticsAdapter logisticsAdapter;
    private List<LogisticsBean.Data.LogisticsListBean> list = new ArrayList<>();
    private String orderId;

    @Override
    public int getContentView() {
        return R.layout.activity_logistics;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("物流信息");
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );

        orderId = getIntent().getStringExtra("orderId");

        logisticsAdapter = new LogisticsAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleview_logistics.setLayoutManager(linearLayoutManager);
        recycleview_logistics.setAdapter(logisticsAdapter);

        initDate();
    }

    private void initDate() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("id", orderId);
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().logistic(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    if (request.code.equals("200")) {
                        list = request.data.list;
                        logisticsAdapter.notifyDataSetChanged();
                        ToastUtil.show(request.msg);
                    } else {
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
                    Log.e(TAG, "error", throwable);
                });
    }

    public class LogisticsAdapter extends RecyclerView.Adapter<LogisticsAdapter.ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(LogisticsActivity.this).inflate(R.layout.item_logistics, null);
            inflate.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {

            if(position==0){
                holder.linearLayout.setVisibility(View.VISIBLE);
                holder.linearLayout1.setVisibility(View.GONE);
                holder.linearLayout2.setVisibility(View.GONE);

                holder.textView100.setText(list.get(position).content);
                holder.textView101.setText(list.get(position).time);
            }else if(position==list.size()-1){
                holder.linearLayout1.setVisibility(View.GONE);
                holder.linearLayout.setVisibility(View.GONE);
                holder.linearLayout2.setVisibility(View.VISIBLE);

                holder.textView104.setText(list.get(position).content);
                holder.textView105.setText(list.get(position).time);
            }else{
                holder.textView102.setText(list.get(position).content);
                holder.textView103.setText(list.get(position).time);

                holder.linearLayout1.setVisibility(View.VISIBLE);
                holder.linearLayout.setVisibility(View.GONE);
                holder.linearLayout2.setVisibility(View.GONE);
            }

        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.imageView55)
            ImageView imageView55;
            @BindView(R.id.textView100)
            TextView textView100;
            @BindView(R.id.textView101)
            TextView textView101;
            @BindView(R.id.imageView56)
            ImageView imageView56;
            @BindView(R.id.textView102)
            TextView textView102;
            @BindView(R.id.textView103)
            TextView textView103;
            @BindView(R.id.imageView57)
            ImageView imageView57;
            @BindView(R.id.textView104)
            TextView textView104;
            @BindView(R.id.textView105)
            TextView textView105;
            @BindView(R.id.linearLayout)
            LinearLayout linearLayout;
            @BindView(R.id.linearLayout1)
            LinearLayout linearLayout1;
            @BindView(R.id.linearLayout2)
            LinearLayout linearLayout2;
            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }
}
